package com.xdl.moudle.utils.util.apibase;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.xdl.moudle.utils.util.DateUtil;

/**
 * Created by xzj on 2015/11/26.
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class APIBaseResult {
	/**
	 * 接口访问成功或者失败的状态码
	 */
	@JsonProperty
	protected int status;
	/**
	 * 接口访问错误的时候返回的错误提示文字，访问成功的时候为Success
	 */
	@JsonProperty
	protected String message;
	/**
	 * 服务端实际返回的数据
	 */
	@JsonProperty
	private Object data;
	@JsonProperty
	private String times;
	// 分页
	@JsonProperty
	private Long totalCount;
	// 弹框
	@JsonProperty
	private AlertBean alertInfo;

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getTimes() {
		return times;
	}

	public void setTimes(String times) {
		this.times = times;
	}

	public Long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Long totalCount) {
		this.totalCount = totalCount;
	}

	public AlertBean getAlertInfo() {
		return alertInfo;
	}

	public void setAlertInfo(AlertBean alertInfo) {
		this.alertInfo = alertInfo;
	}

	public APIBaseResult() {

	}

	private APIBaseResult(int status, String message) {
		this.setStatus(status);
		this.setMessage(message);
		this.setTimes(DateUtil.getCurrentDatetime());
	}
	
	public static APIBaseResult success(Object data) {
		APIBaseResult result = new APIBaseResult(ReturnCode.SUCCESS, ReturnCode.MSG_SUCCESS);
		result.setData(data);
		return result;
	}
	
	public static APIBaseResult error(Object data) {
		APIBaseResult result = new APIBaseResult(ReturnCode.FAIL, ReturnCode.MSG_FAIL);
		result.setData(data);
		return result;
	}
	
	public static APIBaseResult warn(Object data) {
		APIBaseResult result = new APIBaseResult(ReturnCode.WARN, ReturnCode.MSG_WARN);
		result.setData(data);
		return result;
	}

	/**
	 * 获取token状态码
	 * @return
	 */
	public static APIBaseResult getTokenResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.TOKEN, ReturnCode.MSG_TOKEN);
		return result;
	}
	/**
	 * 不合法的user_id
	 * @return
	 */
	public static APIBaseResult getUserErrResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_UID, ReturnCode.MSG_ILLEGAL_UID);
		return result;
	}
	/**
	 * 登录失效或者未登录
	 * @return
	 */
	public static APIBaseResult getLogoutResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.LOGON, ReturnCode.MSG_LOGON);
		return result;
	}
	
	/**
	 * 不合法的凭证
	 * @return
	 */
	public static APIBaseResult getSignResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_DOCUMENT, ReturnCode.MSG_ILLEGAL_DOCUMENT);
		return result;
	}
	/**
	 * 不合法的菜单
	 * @return
	 */
	public static APIBaseResult getMenuResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_MENU, ReturnCode.MSG_ILLEGAL_MENU);
		return result;
	}
	/**
	 * 不合法的按钮
	 * @return
	 */
	public static APIBaseResult getButtonResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_BTN, ReturnCode.MSG_ILLEGAL_BTN);
		return result;
	}
	/**
	 * 不合法的版本号
	 * @return
	 */
	public static APIBaseResult getVersionResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_VERSION, ReturnCode.MSG_ILLEGAL_VERSION);
		return result;
	}
	/**
	 * 不合法的请求参数
	 * @return
	 */
	public static APIBaseResult getParamsResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_PARAMS, ReturnCode.MSG_ILLEGAL_PARAMS);
		return result;
	}
	/**
	 * 不合法的请求格式
	 * @return
	 */
	public static APIBaseResult getFormatResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_FORMAT, ReturnCode.MSG_ILLEGAL_FORMAT);
		return result;
	}
	/**
	 * 请求的ID不合法
	 * @return
	 */
	public static APIBaseResult getIDResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.ILLEGAL_ID, ReturnCode.MSG_ILLEGAL_ID);
		return result;
	}
	/**
	 * 权限不足
	 * @return
	 */
	public static APIBaseResult getNoPermissionResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.NO_PERMISSION, ReturnCode.MSG_NO_PERMISSION);
		return result;
	}
	/**
	 * 服务器错误
	 * @return
	 */
	public static APIBaseResult getCompanyDisableResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.COMPANY_DISABLE, ReturnCode.MSG_COMPANY_DISABLE);
		return result;
	}
	/**
	 * 部门不存在
	 * @return
	 */
	public static APIBaseResult getDepartmentDisableResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.DEPARTMENT_DISABLE, ReturnCode.MSG_DEPARTMENT_DISABLE);
		return result;
	}
	/**
	 * 失效用户在企业中的状态
	 * @return
	 */
	public static APIBaseResult getInvalidUserResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.INVALID_USER, ReturnCode.MSG_INVALID_USER);
		return result;
	}
	/**
	 * 系统繁忙
	 * @return
	 */
	public static APIBaseResult getSystemBusyResult() {
		APIBaseResult result = new APIBaseResult(ReturnCode.SYSTEM_BUSY, ReturnCode.MSG_SYSTEM_BUSY);
		return result;
	}
	
	@Override
	public String toString() {
		return JSONObject.toJSONString(this);
	}
}
